'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var classNames = require('classnames');

function noop() {}

var Switch = function (_Component) {
  (0, _inherits3['default'])(Switch, _Component);

  function Switch(props) {
    (0, _classCallCheck3['default'])(this, Switch);

    var _this = (0, _possibleConstructorReturn3['default'])(this, _Component.call(this, props));

    _initialiseProps.call(_this);

    var checked = false;
    if ('checked' in props) {
      checked = !!props.checked;

    } else if ('value' in props) {
      checked = props.checkedValue === props.value;
    } else {
      checked = !!props.defaultChecked;
    }
    _this.state = { checked: checked };
    return _this;
  }

  Switch.prototype.componentDidMount = function componentDidMount() {
    var _props = this.props,
        autoFocus = _props.autoFocus,
        disabled = _props.disabled;

    if (autoFocus && !disabled) {
      this.focus();
    }
  };

  Switch.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    if ('checked' in nextProps) {
      this.setState({
        checked: !!nextProps.checked
      });

    } else if ('value' in nextProps) {
      this.setState({
        checked: nextProps.checkedValue === nextProps.value
      });
    }
  };

  Switch.prototype.setChecked = function setChecked(checked) {
    if (this.props.disabled) {
      return;
    }
    if (!('checked' in this.props)) {
      this.setState({
        checked: checked
      });
    }


    var _props2 = this.props,
        _props2$checkedValue = _props2.checkedValue,
        checkedValue = _props2$checkedValue === undefined ? true : _props2$checkedValue,
        _props2$unCheckedValu = _props2.unCheckedValue,
        unCheckedValue = _props2$unCheckedValu === undefined ? false : _props2$unCheckedValu;

    var value = checked ? checkedValue : unCheckedValue;
    this.props.onChange(value);
  };

  // Handle auto focus when click switch in Chrome


  Switch.prototype.focus = function focus() {
    this.node.focus();
  };

  Switch.prototype.blur = function blur() {
    this.node.blur();
  };

  Switch.prototype.render = function render() {
    var _classNames;

    var _props3 = this.props,
        className = _props3.className,
        prefixCls = _props3.prefixCls,
        disabled = _props3.disabled,
        loadingIcon = _props3.loadingIcon,
        checkedValue = _props3.checkedValue,
        unCheckedValue = _props3.unCheckedValue,
        checkedChildren = _props3.checkedChildren,
        tabIndex = _props3.tabIndex,
        unCheckedChildren = _props3.unCheckedChildren,
        restProps = (0, _objectWithoutProperties3['default'])(_props3, ['className', 'prefixCls', 'disabled', 'loadingIcon', 'checkedValue', 'unCheckedValue', 'checkedChildren', 'tabIndex', 'unCheckedChildren']);

    var checked = this.state.checked;
    var switchTabIndex = disabled ? -1 : tabIndex || 0;
    var switchClassName = classNames((_classNames = {}, (0, _defineProperty3['default'])(_classNames, className, !!className), (0, _defineProperty3['default'])(_classNames, prefixCls, true), (0, _defineProperty3['default'])(_classNames, prefixCls + '-checked', checked), (0, _defineProperty3['default'])(_classNames, prefixCls + '-disabled', disabled), _classNames));
    return _react2['default'].createElement('span', (0, _extends3['default'])({}, restProps, {
      className: switchClassName,
      tabIndex: switchTabIndex,
      ref: this.saveNode,
      onKeyDown: this.handleKeyDown,
      onClick: this.toggle,
      onMouseUp: this.handleMouseUp
    }), loadingIcon, _react2['default'].createElement('span', { className: prefixCls + '-inner' }, checked ? checkedChildren : unCheckedChildren));
  };

  return Switch;
}(_react.Component);

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.toggle = function () {
    var onClick = _this2.props.onClick;

    var checked = !_this2.state.checked;
    _this2.setChecked(checked);
    onClick(checked);
  };

  this.handleKeyDown = function (e) {
    if (e.keyCode === 37) {
      // Left
      _this2.setChecked(false);
    } else if (e.keyCode === 39) {
      // Right
      _this2.setChecked(true);
    } else if (e.keyCode === 32 || e.keyCode === 13) {
      // Space, Enter
      _this2.toggle();
    }
  };

  this.handleMouseUp = function (e) {
    if (_this2.node) {
      _this2.node.blur();
    }
    if (_this2.props.onMouseUp) {
      _this2.props.onMouseUp(e);
    }
  };

  this.saveNode = function (node) {
    _this2.node = node;
  };
};

Switch.propTypes = {
  className: _propTypes2['default'].string,
  prefixCls: _propTypes2['default'].string,
  disabled: _propTypes2['default'].bool,
  checkedChildren: _propTypes2['default'].any,
  unCheckedChildren: _propTypes2['default'].any,
  onChange: _propTypes2['default'].func,
  onMouseUp: _propTypes2['default'].func,
  onClick: _propTypes2['default'].func,
  tabIndex: _propTypes2['default'].number,
  checked: _propTypes2['default'].bool,
  defaultChecked: _propTypes2['default'].bool,
  autoFocus: _propTypes2['default'].bool,
  loadingIcon: _propTypes2['default'].node,
  checkedValue: _propTypes2['default'].any,
  unCheckedValue: _propTypes2['default'].any
};

Switch.defaultProps = {
  prefixCls: 'rc-switch',
  checkedChildren: null,
  unCheckedChildren: null,
  className: '',
  defaultChecked: false,
  checkedValue: true,
  unCheckedValue: false,
  onChange: noop,
  onClick: noop
};

exports['default'] = Switch;
module.exports = exports['default'];